home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / UResourceParser / TMPLParser ƒ / TMPLParser.cp < prev    next >
Encoding:
Text File  |  1996-10-14  |  6.8 KB  |  295 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    Copyright © 1996 Michael Schürig
  3. //
  4. //    You may copy this file, rip it apart or use it in derivative work as long
  5. //    as you don't change the original file and this message remains intact.
  6. // =================================================================================
  7.  
  8. #include <ansi_prefix.mac.h>
  9. #include <cctype>
  10. #include <iostream>
  11. #include <UException.h>
  12. #include "TMPLParser.h"
  13.  
  14.  
  15. // As of now I can't get the MSL omanip to work, so here's the workaround
  16.  
  17. class indent {
  18.  
  19.     friend ostream &operator<< (ostream &, indent const &);
  20.  
  21. private:
  22.     UInt16    mIndent;
  23.  
  24. public:
  25.     indent(const UInt16 ind): mIndent(ind) {}
  26. };
  27.  
  28.  
  29. UInt8 HexToInt(const char inDigit);
  30.  
  31. ostream &operator<< (ostream &, ConstStr255Param);
  32.  
  33.  
  34.  
  35. TMPLParser::TMPLParser(
  36.     const Handle inTMPLH)
  37.     throw()
  38.     :    UResourceParser(inTMPLH)
  39. {
  40.  
  41. }
  42.  
  43. TMPLParser::~TMPLParser()
  44.     throw()
  45. {
  46. }
  47.  
  48. void
  49. TMPLParser::Parse()
  50. {
  51. }
  52.  
  53. void
  54. TMPLParser::WriteParser(const string &inParserName, ostream & os)
  55. {
  56.     UInt16    listCount = 0;
  57.     UInt16    indLvl = 1;
  58.     
  59.     os << "#include \"UResourceParser.h\"" << endl << endl;
  60.     os << "class " << inParserName << ": public UResourceParser {" << endl;
  61.     os << "public:" << endl;
  62.     os << "\t\t\t\t\t" <<        inParserName << "(const Handle inResH) throw();" << endl;
  63.     os << "\tvirtual\t\t\t~" <<    inParserName << "() throw();" << endl << endl;
  64.     os << "\tvirtual void\tParse();" << endl;
  65.     os << "};" << endl << endl << endl << endl;
  66.     
  67.     
  68.     os << "void" << endl << inParserName << "::Parse()" << endl << "{" << endl;
  69.     
  70.     UResourceParser::ListUntilEnd    itemIter(this);
  71.     
  72.     while (itemIter.Advance())
  73.     {
  74.         StringPtr    label;
  75.         OSType        fieldType;
  76.         
  77.         label = PSTR();
  78.         fieldType = TNAM();
  79.  
  80.         
  81.         switch (fieldType)
  82.         {
  83.             // Alignments
  84.             case 'AWRD':
  85.                 os << indent(indLvl) << "AWRD();" << endl;
  86.                 break;
  87.             case 'ALNG':
  88.                 os << indent(indLvl) << "ALNG();" << endl;
  89.                 break;
  90.             
  91.             // Fillers
  92.             case 'FBYT':
  93.                 os << indent(indLvl) << "FBYT();" << endl;
  94.                 break;
  95.             case 'FWRD':
  96.                 os << indent(indLvl) << "FWRD();" << endl;
  97.                 break;
  98.             case 'FLNG':
  99.                 os << indent(indLvl) << "FLNG();" << endl;
  100.                 break;
  101.             
  102.             // Integers
  103.             case 'DBYT':
  104.             case 'HBYT':
  105.                 os << endl << indent(indLvl) << "// " << label << endl;
  106.                 os << indent(indLvl) << "SBYT();" << endl;
  107.                 break;
  108.             case 'DWRD':
  109.             case 'HWRD':
  110.                 os << endl << indent(indLvl) << "// " << label << endl;
  111.                 os << indent(indLvl) << "SWRD();" << endl;
  112.                 break;
  113.             case 'DLNG':
  114.             case 'HLNG':
  115.                 os << endl << indent(indLvl) << "// " << label << endl;
  116.                 os << indent(indLvl) << "SLNG();" << endl;
  117.                 break;
  118.         
  119.             // Pascal strings
  120.             case 'PSTR':
  121.             case 'ESTR':
  122.             case 'OSTR':
  123.                 os << endl << indent(indLvl) << "// " << label << endl;
  124.                 os << indent(indLvl) << "PSTR();" << endl;
  125.                 break;
  126.             
  127.             // C strings
  128.             case 'CSTR':
  129.             case 'ECST':
  130.             case 'OCST':
  131.                 os << endl << indent(indLvl) << "// " << label << endl;
  132.                 os << indent(indLvl) << "CSTR();" << endl;
  133.                 break;
  134.             
  135.             // Hex dump
  136.             
  137.             case 'HEXD':
  138.                 os << endl << indent(indLvl) << "// " << label << endl;
  139.                 os << indent(indLvl) << "HEXD();" << endl;
  140.                 break;
  141.             
  142.             // Misc
  143.             
  144.             case 'BOOL':
  145.                 os << endl << indent(indLvl) << "// " << label << endl;
  146.                 os << indent(indLvl) << "BOOL();" << endl;
  147.                 break;
  148.             
  149.             case 'BBIT': {
  150.                 UInt8    i = 7;
  151.                 os << endl << indent(indLvl) << "// " << label;
  152.                 while(i--)
  153.                 {
  154.                     label = PSTR();
  155.  
  156.                     OSType theBit = TNAM();
  157.                     if (theBit != 'BBIT')
  158.                         throw "Illegal number of BBITs";
  159.  
  160.                         os << endl << indent(indLvl) << "// " << label;
  161.                 }
  162.                 os << endl << indent(indLvl) << "BBIT();" << endl;
  163.                 
  164.                 break;
  165.             }
  166.             case 'CHAR':
  167.                 os << endl << indent(indLvl) << "// " << label << endl;
  168.                 os << indent(indLvl) << "CHAR();" << endl;
  169.                 break;
  170.             
  171.             case 'RECT':
  172.                 os << endl << indent(indLvl) << "// " << label << endl;
  173.                 os << indent(indLvl) << "RECT();" << endl;
  174.                 break;
  175.             
  176.             case 'TNAM':
  177.                 os << endl << indent(indLvl) << "// " << label << endl;
  178.                 os << indent(indLvl) << "TNAM();" << endl;
  179.                 break;
  180.             
  181.             
  182.             // Lists
  183.             
  184.             case 'LSTB':
  185.                 ++listCount;
  186.                 os << indent(indLvl) << "UResourceParser::ListUntilEnd\titer" << listCount << ";" << endl;
  187.                 os << indent(indLvl) << "while (iter" << listCount << ".Advance())" << endl;
  188.                 os << indent(indLvl) << '{';
  189.                 ++indLvl;
  190.                 break;
  191.             
  192.             case 'LSTZ':
  193.                 ++listCount;
  194.                 os << indent(indLvl) << "UResourceParser::ListUntilZero\titer" << listCount << ";" << endl;
  195.                 os << indent(indLvl) << "while (iter" << listCount << ".Advance())" << endl;
  196.                 os << indent(indLvl) << '{';
  197.                 ++indLvl;
  198.                 break;
  199.  
  200.             case 'OCNT':
  201.                 ++listCount;
  202.                 os << endl << indent(indLvl) << "// " << label << endl;
  203.                 os << indent(indLvl) << "UResourceParser::ListOneBased\titer" << listCount << ";" << endl;
  204.                 os << indent(indLvl) << "while (iter" << listCount << ".Advance())" << endl;
  205.                 os << indent(indLvl) << '{';
  206.                 ++indLvl;
  207.                 break;
  208.             
  209.             case 'ZCNT':
  210.                 ++listCount;
  211.                 os << endl << indent(indLvl) << "// " << label << endl;
  212.                 os << indent(indLvl) << "UResourceParser::ListZeroBased\titer" << listCount << ";" << endl;
  213.                 os << indent(indLvl) << "while (iter" << listCount << ".Advance())" << endl;
  214.                 os << indent(indLvl) << '{';
  215.                 ++indLvl;
  216.                 break;
  217.  
  218.             case 'LSTC':
  219.                 break;
  220.                             
  221.             case 'LSTE':
  222.                 --indLvl;
  223.                 os << indent(indLvl) << '}' << endl;
  224.                 break;
  225.             
  226.             
  227.             default: {    // Fixed-length strings and hexdumps
  228.                 UInt8    flType[4];
  229.                 UInt16    sz;
  230.                 
  231.                 BlockMoveData(&fieldType, &flType, sizeof(fieldType));
  232.                 
  233.                 switch(flType[0])
  234.                 {
  235.                     case 'P':
  236.                         os << endl << indent(indLvl) << "// " << label << endl;
  237.                         sz = (HexToInt(flType[2]) * 16) + HexToInt(flType[3]);
  238.                         os << indent(indLvl) << "P0nn(" << sz << ");" << endl;
  239.                         break;
  240.                     case 'C':
  241.                         os << endl << indent(indLvl) << "// " << label << endl;
  242.                         sz = ((HexToInt(flType[1]) * 16) + (HexToInt(flType[2])) * 16)
  243.                                 + HexToInt(flType[3]);
  244.                         os << indent(indLvl) << "Cnnn(" << sz << ");" << endl;
  245.                         break;
  246.                     case 'H':
  247.                         os << endl << indent(indLvl) << "// " << label << endl;
  248.                         sz = ((HexToInt(flType[1]) * 16) + (HexToInt(flType[2])) * 16)
  249.                                 + HexToInt(flType[3]);
  250.                         os << indent(indLvl) << "Hnnn(" << sz << ");" << endl;
  251.                         break;
  252.                     
  253.                     default:
  254.                         throw "Unknown field type.";
  255.                         break;
  256.                 }
  257.                 
  258.                 break;
  259.             }
  260.         }
  261.     }
  262.     os << '}' << endl;
  263. }
  264.  
  265.  
  266. ostream &operator<< (ostream &os, indent const &ind)
  267. {
  268.     for (UInt16 i = ind.mIndent; i > 0; --i)
  269.         os.put('\t');
  270.     return os;
  271. }
  272.  
  273. ostream &operator<< (ostream &os, ConstStr255Param s)
  274. {
  275.     os.write(static_cast<const char *>(s) + 1, StrLength(s));
  276.     return os;
  277. }
  278.  
  279. UInt8
  280. HexToInt(
  281.     const char inDigit)
  282. {
  283.     UInt8    theValue;
  284.     char    upDigit = toupper(inDigit);    
  285.     
  286.     if ('0' <= inDigit && inDigit <= '9')
  287.         theValue = inDigit - '0';
  288.     else if ('A' <= upDigit && upDigit <= 'F')
  289.         theValue = upDigit - 'A';
  290.     else
  291.         throw "Illegal hex digit.";
  292.  
  293.     return theValue;
  294. }
  295.